package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestLoginVo;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController {
    @Autowired
    ISysUserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody RequestLoginVo vo){
        if(ObjectUtils.isEmpty(vo.getLoginName())){
            return Result.getFailure().setMsg("请填写登录名");
        }
        //查询用户是否正确
        SysUser user = userService.queryUser(vo.getLoginName());
        //
        //用户不存在，loginName判断
        if (ObjectUtils.isEmpty(user)) {
            return Result.getFailure().setMsg("用户不存在");
        }
        //密码错误
        if (!Objects.equals(vo.getPassword(),user.getPassword())) {
            return Result.getFailure().setMsg("密码错误");
        }
        //用户存在且密码正确
        Map<String,Object> result=new HashMap<>();
        result.put("loginName",user.getLoginName());
        return Result.getSuccess().setData(result);
    }

}
